package com.xframework.dao;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
import java.util.Date;
import javax.annotation.Nonnull;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.util.Assert;

import com.xframework.Base;
import com.xframework.annotation.MappingColumn;
import com.xframework.model.*;

public class XJdbcTemplate {
    private static final Logger logger = LoggerFactory.getLogger(XJdbcTemplate.class);
    private static final String DATABASE_TYPE_ORACLE = "ORACLE";
    private static final String DATABASE_TYPE_MYSQL = "MYSQL";
    private static final String JAVA_LANG_STRING = "java.lang.String";
    private static final String JAVA_LANG_INTEGER = "java.lang.Integer";
    private static final String INT = "int";
    private static final String JAVA_LANG_DOUBLE = "java.lang.Double";
    private static final String DOUBLE = "double";
    private static final String JAVA_LANG_LONG = "java.lang.Long";
    private static final String LONG = "long";
    private static final String JAVA_LANG_SHORT = "java.lang.Short";
    private static final String SHORT = "short";
    private static final String JAVA_LANG_FLOAT = "java.lang.Float";
    private static final String FLOAT = "float";
    private static final String JAVA_UTIL_DATE = "java.util.Date";
    private static final String BIGDECIMAL = "java.math.BigDecimal";
    private static final String SQL_TIMESTAMP = "sql.TIMESTAMP";
    private static final String CLOB = "CLOB";
    private static final String NULL = "null";
    private static final String SEQ = "SEQ";
    private static final String SYSDATE = "SYSDATE";
    private static final String SELECT_PAGE_BEGIN_ORACLE = "SELECT * FROM (SELECT ROW_.*, rownum ROWNUM_ FROM (";
    private static final String SELECT_PAGE_END_ORACLE = ") ROW_) WHERE ROWNUM_ > ? AND ROWNUM_ <= ?";
    private static final String SELECT_PAGE_BEGIN_MYSQL = "SELECT * FROM (";
    private static final String SELECT_PAGE_END_MYSQL = ") t LIMIT ?, ?";
    private static final String COMMA_BLANK = ", ";
    private static final String QUESTION = "\\?";
    private static final String QUESTION_COMMA_BLANK = "?, ";
    private static final String INSERT_BLANK_INTO_BLANK = "INSERT INTO ";
    private static final String INSERT = "INSERT";
    private static final String LEFT_BRACKETS = "(";
    private static final String RIGHT_BRACKETS = ")";
    private static final String RIGHT_BRACKETS_BLANK_VALUES_BLANK_LEFT_BRACKETS_BLANK = ") VALUES (";
    private static final String BLANK_EQUAL_BLANK_QUESTION_BLANK_AND_BLANK = " = ? AND ";
    private static final String BLANK_EQUAL_BLANK_QUESTION_COMMA_BLANK = " = ?, ";
    private static final String BLANK_WHERE_BLANK = " WHERE ";
    private static final String BLANK_AND_BLANK = " AND ";
    private static final String UPDATE_BLANK = "UPDATE ";
    private static final String UPDATE = "UPDATE";
    private static final String BLANK_SET_BLANK = " SET ";
    private static final String DELETE_BLANK_FROM_BLANK = "DELETE FROM ";
    private static final String DELETE = "DELETE";
    private static final String SELECT_BLANK = "SELECT ";
    private static final String CURRVAL = ".CURRVAL AS NUM FROM DUAL";
    private static final String NEXTVAL = ".NEXTVAL AS NUM FROM DUAL";
    private static final int ONE = 1;
    private static final int MILLION = 10000;
    private static final String NO_PK_TYPE = "没有指定主键生成方式";
    private static final String PARAM = "param";
    private static final String QUOTATION_BRACE_QUOTATION = "'{}'";
    private String dataBaseType;
    private JdbcTemplate jdbcTemplate;

    public XJdbcTemplate(String dataBaseType, JdbcTemplate jdbcTemplate) {
        this.dataBaseType = dataBaseType;
        this.jdbcTemplate = jdbcTemplate;
    }

    public int getSeqCurrval(String seqName) throws Exception {
        String sql = SELECT_BLANK + seqName + CURRVAL;
        return this.queryForInt(sql);
    }

    public int getSeqNextval(String seqName) throws Exception {
        String sql = SELECT_BLANK + seqName + NEXTVAL;
        return this.queryForInt(sql);
    }

    public int queryForInt(SqlParams sqlParams) throws Exception {
        return this.queryForInt(sqlParams.sql(), sqlParams.params());
    }

    public int queryForInt(String sql, @Nonnull List params) throws Exception {
        return this.queryForInt(sql, params.toArray());
    }

    public int queryForInt(String sql, @Nonnull Object... params) throws Exception {
        RowMapper<Integer> mapper = new BeanPropertyRowMapper<Integer>() {
            @Override
            public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
                return rs.getInt(1);
            }
        };
        int count = this.queryForObject(sql, mapper, params);
        return count;
    }

    public <T extends BaseModel> T queryForObject(SqlParams sqlParams) throws Exception {
        String sql = sqlParams.sql();
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, sqlParams.clazz());
        return this.queryForObject(sql, mapper, sqlParams.params());
    }

    public <T extends BaseModel> T queryForObject(SqlParams sqlParams, Class<T> clazz) throws Exception {
        String sql = sqlParams.sql();
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForObject(sql, mapper, sqlParams.params());
    }

    public <T extends BaseModel> T queryForObject(String sql, Class<T> clazz, @Nonnull Object... params) throws Exception {
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForObject(sql, mapper, params);
    }

    public <T extends BaseModel> T queryForObject(String sql, Class<T> clazz, @Nonnull List params) throws Exception {
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForObject(sql, mapper, params);
    }

    public <T> T queryForObject(String sql, RowMapper<T> mapper, @Nonnull List params) throws Exception {
        return this.queryForObject(sql, mapper, params.toArray());
    }

    public <T> T queryForObject(String sql, RowMapper<T> mapper, @Nonnull Object... params) throws Exception {
        List<T> list = this.queryForList(sql, mapper, null, params);
        T t = null;
        if (list != null && list.size() > 0) {
            t = list.get(0);
        }
        return t;
    }

    /**
     * 数组参数查询返回list
     *
     * @return List
     */
    public <T extends BaseModel> List<T> queryForList(SqlParams sqlParams) throws Exception {
        String sql = sqlParams.sql();
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, sqlParams.clazz());
        return this.queryForList(sql, mapper, sqlParams, sqlParams.params());
    }

    public <T extends BaseModel> List<T> queryForList(SqlParams sqlParams, Class<T> clazz) throws Exception {
        String sql = sqlParams.sql();
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForList(sql, mapper, sqlParams, sqlParams.params());
    }

    public <T extends BaseModel> List<T> queryForList(String sql, Class<T> clazz, BaseModel model, @Nonnull Object... params) throws Exception {
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForList(sql, mapper, model, params);
    }

    public <T extends BaseModel> List<T> queryForList(String sql, Class<T> clazz, BaseModel model, @Nonnull List params) throws Exception {
        RowMapper<T> mapper = this.resultSetToModelRowMapper(sql, clazz);
        return this.queryForList(sql, mapper, model, params);
    }

    public <T> List<T> queryForList(String sql, RowMapper<T> mapper, BaseModel model, @Nonnull List params) throws Exception {
        return this.queryForList(sql, mapper, model, params.toArray());
    }

    public <T> List<T> queryForList(String sql, RowMapper<T> mapper, BaseModel model, @Nonnull Object... params) throws Exception {
        int pageIndex = ONE;
        int pageSize = MILLION;
        if (model != null) {
            if (model.getPageIndex() != null) {
                pageIndex = model.getPageIndex();
            }
            if (model.getPageSize() != null) {
                pageSize = model.getPageSize();
            }
        }
        switch (this.dataBaseType) {
            case DATABASE_TYPE_MYSQL:
                return this.queryForListMysql(sql, mapper, pageIndex, pageSize, params);
            case DATABASE_TYPE_ORACLE:
                return this.queryForListOracle(sql, mapper, pageIndex, pageSize, params);
            default:
                return this.queryForListMysql(sql, mapper, pageIndex, pageSize, params);
        }
    }

    private <T> List<T> queryForListMysql(String sql, RowMapper<T> mapper, int pageIndex, int pageSize, @Nonnull Object... params) {
        sql = SELECT_PAGE_BEGIN_MYSQL + sql + SELECT_PAGE_END_MYSQL;
        pageIndex = (pageIndex - 1) * pageSize;
        return this.queryForList(sql, mapper, pageIndex, pageSize, params);
    }

    private <T> List<T> queryForListOracle(String sql, RowMapper<T> mapper, int pageIndex, int pageSize, @Nonnull Object... params) {
        sql = SELECT_PAGE_BEGIN_ORACLE + sql + SELECT_PAGE_END_ORACLE;
        pageIndex = (pageIndex - 1) * pageSize;
        pageSize = pageIndex * pageSize;
        return this.queryForList(sql, mapper, pageIndex, pageSize, params);
    }

    private <T> List<T> queryForList(String sql, RowMapper<T> mapper, int pageIndex, int pageSize, @Nonnull Object... params) {
        Assert.notNull(params, "params is null");
        int paramsNum = params.length;
        Object[] paramArray = new Object[paramsNum + 2];
        System.arraycopy(params, 0, paramArray, 0, paramsNum);
        paramArray[paramsNum] = pageIndex;
        paramArray[paramsNum + 1] = pageSize;
        this.logParams(sql, paramArray);
        return this.jdbcTemplate.query(sql, mapper, paramArray);
    }

    public <T extends BaseModel> T insert(T model) throws Exception {
        model = this.setModelPkFieldValue(model);
        String tableName = ModelMap.getMappingModel(model.getClass()).getTableName();
        Map<MappingColumn, Object> columnMap = this.getMappingColumnValues(model, INSERT);
        StringBuilder columnStringBuilder = new StringBuilder();
        StringBuilder valueStringBuilder = new StringBuilder();
        List params = new ArrayList();
        columnMap.forEach((mappingColumn, fieldValue) -> {
            if (!mappingColumn.columnReadOnly()) {
                columnStringBuilder.append(mappingColumn.columnName()).append(COMMA_BLANK);
                valueStringBuilder.append(QUESTION_COMMA_BLANK);
                params.add(fieldValue);
            }
        });

        String columnStr = columnStringBuilder.toString();
        String valueStr = valueStringBuilder.toString();
        if (columnStr.length() > 0) {
            columnStr = columnStr.substring(0, columnStr.lastIndexOf(COMMA_BLANK));
        }
        if (valueStr.length() > 0) {
            valueStr = valueStr.substring(0, valueStr.lastIndexOf(COMMA_BLANK));
        }
        String sql = INSERT_BLANK_INTO_BLANK + tableName + LEFT_BRACKETS + columnStr + RIGHT_BRACKETS_BLANK_VALUES_BLANK_LEFT_BRACKETS_BLANK + valueStr + RIGHT_BRACKETS;
        this.execute(sql, params);
        return model;
    }

    public <T extends BaseModel> int update(T model) throws Exception {
        String tableName = ModelMap.getMappingModel(model.getClass()).getTableName();
        Map<MappingColumn, Object> columnMap = this.getMappingColumnValues(model, UPDATE);

        StringBuilder columnStringBuilder = new StringBuilder();
        StringBuilder whereStringBuilder = new StringBuilder();
        List columnParams = new ArrayList();
        List whereParams = new ArrayList();
        columnMap.forEach((mappingColumn, fieldValue) -> {
            if (fieldValue != null) {
                if (mappingColumn.columnPk()) {
                    whereStringBuilder.append(mappingColumn.columnName()).append(BLANK_EQUAL_BLANK_QUESTION_BLANK_AND_BLANK);
                    whereParams.add(fieldValue);
                } else {
                    if (!mappingColumn.columnReadOnly()) {
                        columnStringBuilder.append(mappingColumn.columnName()).append(BLANK_EQUAL_BLANK_QUESTION_COMMA_BLANK);
                        if (String.class.isInstance(fieldValue) && fieldValue.equals(NULL)) {
                            fieldValue = null;
                        }
                        columnParams.add(fieldValue);
                    }
                }
            }
        });

        String columnStr = columnStringBuilder.toString();
        String whereStr = whereStringBuilder.toString();
        if (columnStr.length() > 0) {
            columnStr = columnStr.substring(0, columnStr.lastIndexOf(COMMA_BLANK));
        }
        if (whereStr.length() > 0) {
            whereStr = BLANK_WHERE_BLANK + whereStr.substring(0, whereStr.lastIndexOf(BLANK_AND_BLANK));
            columnParams.addAll(whereParams);
            String sql = UPDATE_BLANK + tableName + BLANK_SET_BLANK + columnStr + whereStr;
            return this.execute(sql, columnParams);
        } else {
            return 0;
        }
    }

    public <T extends BaseModel> int update(T modelSet, T modelWhere) throws Exception {
        String tableName = ModelMap.getMappingModel(modelSet.getClass()).getTableName();
        Map<MappingColumn, Object> setColumnMap = this.getMappingColumnValues(modelSet, UPDATE);
        Map<MappingColumn, Object> whereColumnMap = this.getMappingColumnValues(modelWhere, SELECT_BLANK);

        StringBuilder columnStringBuilder = new StringBuilder();
        StringBuilder whereStringBuilder = new StringBuilder();
        List params = new ArrayList();
        setColumnMap.forEach((mappingColumn, fieldValue) -> {
            if (!mappingColumn.columnReadOnly()) {
                if (fieldValue != null) {
                    columnStringBuilder.append(mappingColumn.columnName()).append(BLANK_EQUAL_BLANK_QUESTION_COMMA_BLANK);
                    if (String.class.isInstance(fieldValue) && fieldValue.equals(NULL)) {
                        fieldValue = null;
                    }
                    params.add(fieldValue);
                }
            }
        });
        whereColumnMap.forEach((mappingColumn, fieldValue) -> {
            if (fieldValue != null) {
                whereStringBuilder.append(mappingColumn.columnName()).append(BLANK_EQUAL_BLANK_QUESTION_BLANK_AND_BLANK);
                params.add(fieldValue);
            }
        });

        String columnStr = columnStringBuilder.toString();
        String whereStr = whereStringBuilder.toString();
        if (columnStr.length() > 0) {
            columnStr = columnStr.substring(0, columnStr.lastIndexOf(COMMA_BLANK));
        }
        if (whereStr.length() > 0) {
            whereStr = BLANK_WHERE_BLANK + whereStr.substring(0, whereStr.lastIndexOf(BLANK_AND_BLANK));
            String sql = UPDATE_BLANK + tableName + BLANK_SET_BLANK + columnStr + whereStr;
            return this.execute(sql, params);
        } else {
            return 0;
        }
    }

    public <T extends BaseModel> int delete(T model) throws Exception {
        String tableName = ModelMap.getMappingModel(model.getClass()).getTableName();
        Map<MappingColumn, Object> columnMap = this.getMappingColumnValues(model, DELETE);
        StringBuilder whereStringBuilder = new StringBuilder();
        List params = new ArrayList();
        columnMap.forEach((mappingColumn, fieldValue) -> {
            if (!mappingColumn.columnReadOnly()) {
                if (fieldValue != null) {
                    whereStringBuilder.append(mappingColumn.columnName()).append(BLANK_EQUAL_BLANK_QUESTION_BLANK_AND_BLANK);
                    params.add(fieldValue);
                }
            }
        });

        String whereStr = whereStringBuilder.toString();
        if (whereStr.length() > 0) {
            whereStr = BLANK_WHERE_BLANK + whereStr.substring(0, whereStr.lastIndexOf(BLANK_AND_BLANK));
            String sql = DELETE_BLANK_FROM_BLANK + tableName + whereStr;
            return this.execute(sql, params);
        } else {
            return 0;
        }
    }

    public int execute(String sql, @Nonnull List params) throws Exception {
        return this.execute(sql, params.toArray());
    }

    public int execute(String sql, @Nonnull Object... params) throws Exception {
        this.logParams(sql, params);
        Assert.notNull(params, "params is null");
        if (params.length == 0) {
            return this.jdbcTemplate.update(sql);
        } else {
            return this.jdbcTemplate.update(sql, params);
        }
    }

    /**
     * 调用存储过程
     *
     * @param sql      String
     * @param inParams Object[]
     * @param inTypes  int[]
     * @param outTypes int[]
     * @return Object[]
     */
    public Object[] callableStatement(final String sql, final Object[] inParams, final int[] inTypes, final int[] outTypes) throws Exception {
        this.logParams(sql, inParams);
        CallableStatementCallback cb = (CallableStatement cs) -> {
//		CallableStatementCallback cb = new CallableStatementCallback() {
//			@Override
//			public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
            int inLen = 0;
            if (inParams != null && inTypes != null) {
                inLen = inParams.length;
                for (int i = 0; i < inLen; i++) {
                    cs.setObject(i + 1, inParams[i], inTypes[i]);
                }
            }
            int outLen = 0;
            if (outTypes != null) {
                outLen = outTypes.length;
                for (int i = inLen; i < inLen + outLen; i++) {
                    cs.registerOutParameter(i + 1, outTypes[i - inLen]);
                }
            }

//            int inLen = inParams.length;
//            int outLen = outParams.length;
//            int i;
//            this.logParams(sql, inParams);
//            for (i = 0; i < inLen; i++) {
//                cs.setObject(i + 1, inParams[i], inTypes[i]);
//            }
//            for (i = inLen; i < inLen + outLen; i++) {
//                cs.registerOutParameter(i + 1, outTypes[i - inLen]);
//            }
            cs.execute();
            Object[] outParams = new Object[outLen];
            for (int i = 0; i < outLen; i++) {
                outParams[i] = cs.getObject(inLen + i + 1);
            }
            return outParams;
//			}
        };
        Object obj = this.jdbcTemplate.execute(sql, cb);
        return (Object[]) obj;
    }

    /**
     * 把ResultSet转化成map，再把map转化成model，再把model转化成RowMapper
     *
     * @return RowMapper
     * @throws SQLException
     */
    private <T extends BaseModel> RowMapper<T> resultSetToModelRowMapper(final String sql, final Class<T> modelClass) {
        RowMapper<T> mapper = new BeanPropertyRowMapper<T>() {
            @Override
            public T mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    MappingModel<T> mappingModel = ModelMap.getMappingModel(modelClass);
                    Map<String, String> columnFieldMap = mappingModel.getColumnFieldMap();
                    Map<String, Object> valueMap = new HashMap<String, Object>();
                    ResultSetMetaData rsmd = rs.getMetaData();
                    int columnCount = rsmd.getColumnCount();
                    for (int i = 1; i <= columnCount; i++) {
                        String columnName = rsmd.getColumnName(i).toUpperCase();
                        String fieldName = columnFieldMap.get(columnName);
                        String columnClassName = rsmd.getColumnClassName(i);
                        Object columnValue = rs.getObject(i);
                        if (fieldName != null && columnClassName != null && columnValue != null) {
                            if (columnClassName.equals(JAVA_LANG_STRING)) {
                                columnValue = columnValue.toString();
                            } else if (columnClassName.equals(BIGDECIMAL)) {
                                columnValue = rs.getBigDecimal(i);
                            } else if (columnClassName.toUpperCase().indexOf(SQL_TIMESTAMP) > -1) {
                                columnValue = new Date(rs.getTimestamp(i).getTime());
                            } else if (columnClassName.toUpperCase().indexOf(CLOB) > -1) {
                                Clob clob = (Clob) columnValue;
                                columnValue = clob.getSubString(1, (int) clob.length());
                            }
                            valueMap.put(fieldName, columnValue);
                        }
                    }

                    T modelInstance = modelClass.newInstance();

                    Map<String, List<MappingModel>> mappingModelListMap = mappingModel.getMappingModelListMap();
                    List<MappingModel> mappingFieldModelList = mappingModelListMap.get(sql);
                    String upperSql = sql.toUpperCase();
                    List<MappingModel> list = null;
                    if (mappingFieldModelList == null) {
                        mappingFieldModelList = mappingModel.getMappingModelList();
                        list = new ArrayList<MappingModel>();
                    }
                    String tableName;
                    for (MappingModel mappingFieldModel : mappingFieldModelList) {
                        tableName = mappingFieldModel.getTableName() + Base.SPACE;
                        if (upperSql.contains(tableName)) {
                            BaseModel fieldInstance = (BaseModel) mappingFieldModel.getFieldType().newInstance();
                            BeanUtils.populate(fieldInstance, valueMap);
                            valueMap.put(mappingFieldModel.getFieldName(), fieldInstance);

                            upperSql = upperSql.replaceFirst(tableName, Base.BLANK);
                            if (list != null) {
                                list.add(mappingFieldModel);
                            }
                        }
                    }
                    if (list != null) {
                        mappingModelListMap.put(sql, list);
                        mappingModel.setMappingModelListMap(mappingModelListMap);
                    }
                    BeanUtils.populate(modelInstance, valueMap);
                    return modelInstance;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
        return mapper;
    }

    /**
     * 设置model的主键值
     *
     * @param model BaseModel
     * @return BaseModel
     */
    private <T extends BaseModel> T setModelPkFieldValue(T model) throws Exception {
        List<MappingField> mappingFieldList = ModelMap.getMappingModel(model.getClass()).getMappingFieldList();
        for (MappingField mappingField : mappingFieldList) {
            if (mappingField.getColumnPk()) {
                Field field = mappingField.getField();
                Object uid = field.get(model);
                if (uid == null || uid.toString().length() == 0) {
                    String id;
                    if (mappingField.getColumnPkType().equals(Base.BLANK)) {
                        id = Base.getUid(mappingField.getColumnLength());
                    } else if (mappingField.getColumnPkType().equals(SEQ)) {
                        id = this.getSeqNextval(mappingField.getColumnPkSeq()) + Base.BLANK;
                    } else {
                        throw new RuntimeException(NO_PK_TYPE);
                    }
                    String fieldClassTypeName = mappingField.getFieldClassTypeName();
                    if (fieldClassTypeName.equals(JAVA_LANG_STRING)) {
                        uid = id;
                    } else if (fieldClassTypeName.equals(JAVA_LANG_INTEGER) || fieldClassTypeName.equals(INT)) {
                        uid = Integer.parseInt(id);
                    } else {
                        uid = id;
                    }
                    field.set(model, uid);
                }
            }
        }
        return model;
    }

    /**
     * 返回model所有属性注解及属性值集合
     *
     * @param model BaseInterface
     * @return Map
     */
    private <T extends BaseModel> Map<MappingColumn, Object> getMappingColumnValues(T model, String flag) {
        Map<MappingColumn, Object> columnMap = new LinkedHashMap<MappingColumn, Object>();
        try {
            List<MappingField> mappingFieldList = ModelMap.getMappingModel(model.getClass()).getMappingFieldList();
            Field field;
            for (MappingField mappingField : mappingFieldList) {
                if (!mappingField.getColumnOnlySelect()) {
                    String fieldClassTypeName = mappingField.getFieldClassTypeName();
                    field = mappingField.getField();
                    Object fieldValue = field.get(model);
                    if (Base.isNotNull(fieldValue)) {
//                        if (flag.equals(INSERT) || flag.equals(UPDATE)) {
//                            switch (fieldClassTypeName) {
//                                case JAVA_LANG_STRING:
//                                    fieldValue = columnDefaultValue;
//                                case JAVA_LANG_INTEGER:
//                                    fieldValue = Integer.parseInt(columnDefaultValue);
//                                case INT:
//                                    fieldValue = Integer.parseInt(columnDefaultValue);
//                                case JAVA_LANG_DOUBLE:
//                                    fieldValue = Double.parseDouble(columnDefaultValue);
//                                case DOUBLE:
//                                    fieldValue = Double.parseDouble(columnDefaultValue);
//                                case JAVA_LANG_FLOAT:
//                                    fieldValue = Float.parseFloat(columnDefaultValue);
//                                case FLOAT:
//                                    fieldValue = Float.parseFloat(columnDefaultValue);
//                                case JAVA_LANG_LONG:
//                                    fieldValue = Long.parseLong(columnDefaultValue);
//                                case LONG:
//                                    fieldValue = Long.parseLong(columnDefaultValue);
//                                case JAVA_LANG_SHORT:
//                                    fieldValue = Short.parseShort(columnDefaultValue);
//                                case SHORT:
//                                    fieldValue = Short.parseShort(columnDefaultValue);
//                                case JAVA_UTIL_DATE:
//                                    fieldValue = new Date();
//                                case SYSDATE:
//                                    fieldValue = new Date();
//                                default:
//                                    fieldValue = columnDefaultValue;
//                            }
//                        }
                        columnMap.put(mappingField.getMappingColumn(), fieldValue);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return columnMap;
    }

    /**
     * 记录sql参数
     *
     * @param params Object[]
     */
    private void logParams(String sql, Object... params) {
        sql = sql.replaceAll(QUESTION, QUOTATION_BRACE_QUOTATION);
        logger.info(sql, params);
        int i = 1;
        for (Object param : params) {
            logger.info(PARAM + (i++) + Base.SPACE + QUOTATION_BRACE_QUOTATION, param);
        }
    }

}
